﻿using Common;
using IBll;
using MainService.Controllers.Extend;
using ResponseModel;
using System;
using System.Web.Http;

namespace MainService.Controllers
{
    /// <summary>
    /// 通用服务控制器
    /// </summary>
    public class CommonServiceController : BaseApiController
    {
        public ICommonServiceBll CommonServiceBll { get; set; }

        /// <summary>
        /// 根据服务编码调用服务（入参Demo:{"ServiceCode":"001","其他参数":"参数值"}）
        /// 提示：服务主要由主服务来提供，通用服务只提供既定的较少的（如随场景变化而变化的需求接口）且较为简单的服务。切记：通用服务不可滥用，不然违背设计之初，且本身此做法就不合理。
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel CallServiceWithCode(dynamic param)
        {
            ResultModel result = new ResultModel();
            string serviceCode = param.ServiceCode;
            LogHelper.Debug("入参:" + param);

            try
            {
                if (!string.IsNullOrEmpty(serviceCode))
                {
                    string response = "";
                    if (CommonServiceBll.ExecuteService(param, out response))
                    {
                        result.Pass = true;
                        result.Msg = "服务编码为" + serviceCode + "的服务执行成功";
                        result.Append = response;
                    }
                    else
                    {
                        result.Pass = false;
                        result.Msg = "服务编码为" + serviceCode + "的服务执行失败";
                        result.Append = response;
                    }
                }
                else
                {
                    result.Pass = false;
                    result.Msg = "服务编码为空，请检查入参格式。";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("服务编码为" + serviceCode + "的服务发生异常：" + ex);
                result.Pass = false;
                result.Msg = "服务编码为" + serviceCode + "的服务发生异常：" + ex;
            }
            LogHelper.Debug("服务编码为" + serviceCode + "的服务最终的返回结果：" + result.ToJson());

            return result;
        }

        /// <summary>
        /// 根据服务编码重加载服务
        /// </summary>
        /// <param name="serviceCode"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel ReloadServiceWithCode(string serviceCode)
        {
            ResultModel result = new ResultModel();

            try
            {
                if (!string.IsNullOrEmpty(serviceCode))
                {
                    if (CommonServiceBll.ReloadService(serviceCode))
                    {
                        result.Pass = true;
                        result.Msg = "服务编码为" + serviceCode + "的服务重加载成功";
                    }
                    else
                    {
                        result.Pass = false;
                        result.Msg = "服务编码为" + serviceCode + "的服务重加载失败";
                    }
                }
                else
                {
                    result.Pass = false;
                    result.Msg = "服务编码为空，请检查入参格式。";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("服务编码为" + serviceCode + "的服务重加载发生异常：" + ex);
                result.Pass = false;
                result.Msg = "服务编码为" + serviceCode + "的服务重加载发生异常：" + ex;
            }
            LogHelper.Debug("服务编码为" + serviceCode + "的服务重加载的返回结果：" + result.ToJson());

            return result;
        }
    }
}
